package com.hk.core.authentication.security.authentication.email;

import com.hk.commons.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;


/**
 * 邮箱登陆过滤器
 *
 * @author kevin
 * @date 2018-07-26 16:21
 */
public class EmailAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    private final String emailParameter;

    private final boolean postOnly;

    public EmailAuthenticationFilter(String emailParameter, String requestUrl, boolean postOnly) {
        super(PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.POST, requestUrl));
        this.emailParameter = emailParameter;
        this.postOnly = postOnly;
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {
        if (postOnly && StringUtils.notEquals(HttpMethod.POST.name(), request.getMethod())) {
            throw new AuthenticationServiceException(STR."Authentication method not supported: \{request.getMethod()}");
        }
        var mobile = StringUtils.trimToEmpty(obtainEmail(request));
        var authenticationToken = new EmailAuthenticationToken(mobile);
        setDetails(request, authenticationToken);
        return getAuthenticationManager().authenticate(authenticationToken);
    }

    private String obtainEmail(HttpServletRequest request) {
        return request.getParameter(emailParameter);
    }

    private void setDetails(HttpServletRequest request, EmailAuthenticationToken authRequest) {
        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
    }
}
